package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Inquiry;
import com.b2b.model.entity.InquiryMaterial;
import com.b2b.model.entity.RequirementOfMaterial;
import com.b2b.util.RequirementOfMaterialUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/requirementOfMaterial")
@PropertySource("classpath:config.properties")
public class RequirementOfMaterialController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;
    private final RequirementOfMaterialUtil requirementOfMaterialUtil;

    @Autowired
    public RequirementOfMaterialController(RequirementOfMaterialUtil requirementOfMaterialUtil){
        this.requirementOfMaterialUtil = requirementOfMaterialUtil;
    }

    @GetMapping("/getAll")
    public ResponseBean getAll(InquiryMaterial inquiryMaterial){
        Integer inquiryId = inquiryMaterial.getInquiryId();
        List<RequirementOfMaterial> requirementOfMaterialList = requirementOfMaterialUtil.getAll(inquiryId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", requirementOfMaterialList);
    }


    @GetMapping("/geyById")
    public ResponseBean getRequirement(@RequestBody InquiryMaterial inquiryMaterial){
        Integer requirementId = inquiryMaterial.getRequirementId();
        RequirementOfMaterial requirementOfMaterial = new RequirementOfMaterial();
        requirementOfMaterial = requirementOfMaterialUtil.getByRequirementId(requirementId);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", requirementOfMaterial);
    }

    @DeleteMapping
    public ResponseBean deleteInquiry(@RequestBody InquiryMaterial inquiryMaterial){
        Integer requirementId = inquiryMaterial.getInquiryId();
        Integer count = requirementOfMaterialUtil.deleteInquiry(requirementId);
        if (count <= 0 ){
            throw new CustomException("删除失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Query was successful)", null);
    }

    @PutMapping
    public ResponseBean updateInquiry(@RequestBody RequirementOfMaterial requirementOfMaterial){
        Integer count = requirementOfMaterialUtil.updateInquiry(requirementOfMaterial);
        if (count <= 0 ){
            throw new CustomException("更新失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Query was successful)", null);
    }
}
